8VC Venture Cup 2016 - Final Round (Div. 2 Edition) C. XOR Equation 数学
C. XOR Equation
题目连接:
http://www.codeforces.com/contest/635/problem/C
Description
Two positive integers a and b have a sum of s and a bitwise XOR of x. How many possible values are there for the ordered pair (a, b)?
Input
The first line of the input contains two integers s and x (2 ≤ s ≤ 1012, 0 ≤ x ≤ 1012), the sum and bitwise xor of the pair of positive integers, respectively.
Output
Print a single integer, the number of solutions to the given conditions. If no solutions exist, print 0.
Sample Input
9 5
Sample Output
4
Hint
题意
给你两个数的和,以及两个数的异或结果,问你有多少个数对a,b满足要求
题解:
首先你要知道 a+b = a&b*2 + a^b,这个东西是递归加法的定义
所以你就知道了a&b和a^b,然后根据这两个东西对于数字的每一位进行讨论就好了
如果a^b = 1,那么a&b必须等于0,否则肯定不对嘛,这时候,ai=1 bi=0,ai=0 bi=1有两种选择
如果a^b = 0,那么a&b = ai = bi,只有一种选择
所以答案就是2的a^b中1的个数次方。当然,最后还得判一判s==x的情况,这种情况得把0的情况给剖去。
代码
#include<bits/stdc++.h>
using namespace std;
long long s,x;
int flag = 0;
int main()
{
cin>>s>>x;
if(s==x)flag = 1;
s-=x;
if(s%2==1)return puts("0");
s/=2;
long long ans = 1;
for(int i=0;i<60;i++)
{
int p1 = (s>>i)&1;
int p2 = (x>>i)&1;
if(p2==1&&p1==1)return puts("0");
if(p2==1)ans*=2;
}
if(flag)ans-=2;
cout<<ans<<endl;
}